#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qrencode.h>
#include <QBrush>
#include <QMessageBox>
#include <QTextStream>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qrstring = "http://www.google.com";
    height = 0.8;
    wqr = 0.8;
    filename = "";
    shape = 0;
    qrcode = NULL;
    qrlevel = QR_ECLEVEL_L;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::makeqrcode()
{
    if (qrstring == NULL)
           return;

    if (qrcode != NULL)
        QRcode_free(qrcode);

    qrcode = QRcode_encodeString(qrstring.toLocal8Bit().data(),
                      0,qrlevel, QR_MODE_8, 0);

}

void MainWindow::paintEvent(QPaintEvent*)
{

    QPainter painter(this);

    makeqrcode();

    if (qrcode == NULL)
        return;

    int w = qrcode->width;
    int d = 200/w;

    QRect rec(20,50,w*d+20,w*d+20), rec2;
    QBrush brush(Qt::black);

    painter.drawRect(rec);
    painter.setBrush(brush);

    // Scan rows-columns
    for(int i=0; i<w; i++) {
        for(int j=0; j<w; j++){
            if (qrcode->data[j+i*w] & 0x01) {

                rec = QRect(30+i*d,60+j*d, (3*d)/4, (3*d)/4);
                rec2 = QRect(30+i*d,60+j*d, d, d);
                if ((j>=0 && j<=7 && i<=7 && i>=0 ) ||
                    (j>=0 && j<=7 && w-i<=7 && w-i>=0) ||
                    (w-j>=0 && w-j<=7 && i<=7 && i>=0))
                    painter.drawEllipse(rec);
                else
                    painter.drawEllipse(rec);
                //painter.fillRect(rec, Qt::SolidPattern);
            }
        }
    }

}



void MainWindow::on_actionInsert_String_triggered()
{
    double shift;

    filename = QFileDialog::getSaveFileName(
                this, tr("Salva File"),
                "C://", "Autolisp (*.lsp);;All file (*.*)");

    QFile mFile(filename);

    if (!mFile.open(QFile::WriteOnly | QFile::Text)) {
           return;
    }

    QTextStream out(&mFile);

    if (qrcode == NULL )
        return;

    shift = (1-wqr)/2;

    // Scan rows-columns
    for(int i=0; i<qrcode->width; i++) {
        for(int j=0; j<qrcode->width; j++){
            if ((qrcode->data[j+i*qrcode->width] & 0x01)) {
                switch(shape){
                case 0:
                    out << "(command \"_box\" ";
                    out << "\"" << (double(i)+shift) << "," << (double(j)+shift)  << ",0\" " ;
                    out << "\"" << (double(i)+shift+wqr) << "," << (double(j)+shift+wqr)  << "," << height << "\" " ;
                    out <<")\n";
                    break;
                case 1:
                    out << "(command \"cilindro\" ";
                    out << "\"" << i << "," << j  << ",0\" " ;
                    out << "\"0.5\"  \"1\")\n";
                    break;
                case 2:
                    out << "(command \"cilindro\" ";
                    out << "\"" << i << "," << j  << ",0\" " ;
                    out << "\"0.5\"  \"1\")\n";
                    break;
                }
            }
        }
    }

    out << "(command \"_box\" " << "\"-5,-5,0\" \"";
    out << qrcode->width+5 << "," << qrcode->width+5;
    out << "," << height+1 << "\")";

    mFile.flush();
    mFile.close();

}

void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
    qrstring = arg1;
    repaint();
}


void MainWindow::on_comboBox_currentIndexChanged(int index)
{
   // QMessageBox::information(this,"HI","WORLD" + QString::number(index));

    switch (index) {
    case 0:
        qrlevel = QR_ECLEVEL_L;
        break;
    case 1:
        qrlevel = QR_ECLEVEL_M;
        break;
    case 2:
        qrlevel = QR_ECLEVEL_Q;
        break;
    case 3:
        qrlevel = QR_ECLEVEL_H;
        break;
    default:
        break;
    }
    repaint();
}

void MainWindow::on_comboBox_2_currentIndexChanged(int index)
{
    shape = index;
}

void MainWindow::on_lineEdit_2_textChanged(const QString &arg1)
{
    height = arg1.toDouble();
}

void MainWindow::on_lineEdit_3_textChanged(const QString &arg1)
{
    wqr = arg1.toDouble();
}
